﻿Imports System.IO
Public Class frmNewPRItem
    Dim Strsql As String
    Dim _mysql As New ClsSQLhelper
    Dim On_load As Boolean = True
    Public Primary_Key As Integer = -1
    Public New_Rows As Integer = 1
    Dim DTSection As New DataTable
#Region "Function"
    Sub Load_ProductGroup()
        Try
            Strsql = "Select section_id,section_name from section order by readonly desc,section_name"

            DTSection = _Mysql.GetMYSQLDataTable(Strsql, "section")
            With Me.Cbosection
                .DataSource = DTSection
                .DisplayMember = "section_name"
                .ValueMember = "section_id"
            End With
            Cbosection.SelectedValue = My.Settings.Current_Item_Section
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


    End Sub
    Sub Load_ProductType(Optional ByVal ProductGroup As Integer = 0)
        Try
            Strsql = "Select Category_id,Category_name from category where sectionid = " & ProductGroup & " order by readonly desc,Category_name"
            Dim DTcategory As New DataTable
            DTcategory = _Mysql.GetMYSQLDataTable(Strsql, "category")
            With Me.CboCategory
                .DataSource = DTcategory
                .DisplayMember = "Category_name"
                .ValueMember = "Category_id"
            End With
            CboCategory.SelectedValue = My.Settings.Current_Item_Category
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
    Sub Update_Last_Item_Price()
        Try
            Strsql = "Update itemtype set cost = " & Me.TxtPriceIncludeVat.EditValue & " where  doc_detail_id = " & Primary_Key
            _mysql.MySQLExecute(Strsql)


        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Update_Last_Item_Price"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Company()
        Try
            Strsql = "Select * from vendorcompany order by companyname"
            Dim DT_ItemType As New DataTable
            DT_ItemType = _mysql.GetMYSQLDataTable(Strsql, "vendorcompany")
            With CboCompany

                .DataSource = DT_ItemType
                .DisplayMember = "companyname"
                .ValueMember = "companyid"

            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Sub Copy_Image2Server()
        Dim StrFilename As String = ""
        Try

            StrFilename = My.Settings.ImgSharePart & "\" & Now.Year.ToString & Now.Month.ToString & Now.Day.ToString & Now.Hour & Now.Minute & Now.Second & ".jpg"

            File.Copy(Me.TxtImagepart.Text, StrFilename, True)
            Dim bm As New Bitmap(Me.TxtImagepart.Text)
            PictureEdit1.Image = bm
            Me.TxtImagepart.Text = StrFilename
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Copy_Image2Server"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = "StrFilename = " & StrFilename
                .ShowDialog()
            End With
        End Try

    End Sub
    Sub Update_Price()
        Me.TxtPriceIncludeVat.EditValue = Me.TxtPrice.Value + ((Me.TxtPrice.Value * Me.TxtVat.Value) / 100)
        Me.TxtTotalprice.EditValue = Me.TxtPriceIncludeVat.EditValue * Me.TxtQty.EditValue

    End Sub
    Sub Insert_Data()
        Try

            If MsgBox("คุณต้องการเพิ่มข้อมูลใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการเพิ่มข้อมูล") = MsgBoxResult.No Then Exit Sub
            If New_Rows = -1 Then
                New_Rows = 1
            Else
                New_Rows = New_Rows + 1
            End If

            Strsql = "Insert into pr_detail (doc_head_id,company_id,doc_rowid,item_type_id,serialno,band,model,priceincludevat,drawqty,receiveqty,totalprice,staff,remarks,warantry,vat,nonvat,priceperunit,nonprice,purchase_date,nonbuydate,mdf_date,nonmfd,imagepart) "
            Strsql = Strsql & " values(" & Primary_Key & "," & Me.CboCompany.SelectedValue & "," & New_Rows & "," & CboItemType.SelectedValue & ",'" & Me.TxtSerialno.Text & "','" & Me.TxtBand.Text & "','" & Me.TxtModel.Text & "'"
            Strsql = Strsql & " ," & Me.TxtPriceIncludeVat.Value & "," & Me.TxtQty.Value & "," & Me.TxtReceiveQty.Value & "," & Me.TxtTotalprice.Value & ",'" & Me.TxtStaff.Text
            Strsql = Strsql & "','" & Me.TxtRemarks.Text & "'," & Me.TxtWarantry.Value

            'Check nonvat
            If ChkNonePrice.Checked = True Then
                Strsql = Strsql & ", 0,'Y'"
            Else
                Strsql = Strsql & "," & Me.TxtVat.Value & " ,'N'"
            End If

            'Check nonprice
            If ChkNonePrice.Checked = True Then
                Strsql = Strsql & ",  0,  'Y'"
            Else
                Strsql = Strsql & ", " & Me.TxtPrice.Text & ", 'N'"
            End If

            'Check nonbuydate 
            If ChkNoneBuyDate.Checked = True Then
                Strsql = Strsql & ",  null,  'Y'"
            Else
                Strsql = Strsql & ",  '" & Pn_Framework.MysqlDateTimeFormat(Me.DtpPHDate.DateTime, False) & "',  'N'"
            End If

            'Check nonmfd
            If ChkNonMFD.Checked = True Then
                Strsql = Strsql & ",mdf_date= null,nonmfd = 'Y'"
            Else
                Strsql = Strsql & ",mdf_date='" & Pn_Framework.MysqlDateTimeFormat(Me.DtpMFD.DateTime, False) & "',nonmfd = 'N'"
            End If


            If Me.TxtImagepart.Text = "" Then
                Strsql = Strsql & ",''"
            Else
                Copy_Image2Server()
                Strsql = Strsql & ",'" & Replace(Me.TxtImagepart.Text, "\", "\\") & "'"
            End If



            Strsql = Strsql & ")"

            If _mysql.MySQLExecute(Strsql) > 0 Then
                MsgBox("เพิ่มข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการบันทึกข้อมูล")
                Me.DialogResult = Windows.Forms.DialogResult.OK
            Else
                MsgBox("ไม่สามารถเพิ่มข้อมูลได้", MsgBoxStyle.Information, "ผลการบันทึกข้อมูล")
                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "BtnSave_Click"
                    .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง SQL"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()
                End With

            End If

        Catch ex As Exception

            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnSave_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_ItemType()
        Try
            Strsql = "Select * from itemtype "
            Strsql = Strsql & " where section = " & Me.Cbosection.SelectedValue & " and category = " & CboCategory.SelectedValue & "  order by itemname"
            Dim DT_ItemType As New DataTable
            DT_ItemType = _mysql.GetMYSQLDataTable(Strsql, "itemname")
            With CboItemType

                .DataSource = DT_ItemType
                .DisplayMember = "itemname"
                .ValueMember = "itemnumber"

            End With

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_ItemType"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
#End Region
    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Insert_Data()


    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Dispose()
    End Sub

    Private Sub frmNewReceiveItem_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If Me.DialogResult <> Windows.Forms.DialogResult.OK Then
            If MsgBox("คุณต้องการออกโดยไม่บันทึกการแก้ไขใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.No Then
                Me.DialogResult = Windows.Forms.DialogResult.Cancel
                e.Cancel = True
            End If
        End If
    End Sub


    Private Sub frmNewReceiveItem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.TxtStaff.Text = frmNewPRForm.TxtStaff.Text
        DtpPHDate.DateTime = Now
        Load_Company()
        Load_ProductGroup()
        Load_ProductType(Cbosection.SelectedValue)
        Load_ItemType()
        Me.On_load = False

    End Sub
    Private Sub TxtQty_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtQty.EditValueChanged
        Update_Price()
        Me.TxtReceiveQty.Text = TxtQty.Text
    End Sub
    Private Sub TxtPriceIncludeVat_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtPriceIncludeVat.EditValueChanged
        Me.TxtTotalprice.Value = Me.TxtPriceIncludeVat.Value * Me.TxtQty.Value
        Me.TxtReceiveQty.Value = Me.TxtQty.Value
    End Sub

    Private Sub TxtPrice_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtPrice.EditValueChanged
        Update_Price()
    End Sub

    Private Sub TxtReceiveQty_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtReceiveQty.EditValueChanged

    End Sub

    Private Sub BtnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBrowse.Click
        Try
            With Me.OpenFileDialog1
                .Title = "เลือกรูปภาพ"
                .FileName = ""
                .Filter = "Jpeg Image|*.jpg"
                If .ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub

                Me.TxtImagepart.Text = .FileName
                Dim bm As New Bitmap(Me.TxtImagepart.Text)
                PictureEdit1.Image = bm

            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "PicLoadImg_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub ChkNonePrice_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkNonePrice.CheckedChanged
        If On_load = True Then Exit Sub
        Select Case Me.TxtPrice.Enabled
            Case True
                Me.TxtPrice.Enabled = False
            Case False
                Me.TxtPrice.Enabled = True

        End Select
    End Sub

    Private Sub ChkNonVat_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkNonVat.CheckedChanged
        If On_load = True Then Exit Sub
        Select Case Me.TxtVat.Enabled
            Case True
                Me.TxtVat.Enabled = False
                Me.TxtVat.EditValue = 0
            Case False
                Me.TxtVat.Enabled = True
                Me.TxtVat.EditValue = 7

        End Select
    End Sub

    Private Sub ChkNoneBuyDate_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkNoneBuyDate.CheckedChanged
        If On_load = True Then Exit Sub
        Select Case Me.DtpPHDate.Enabled
            Case True
                Me.DtpPHDate.Enabled = False
            Case False
                Me.DtpPHDate.Enabled = True

        End Select
    End Sub

    Private Sub ChkNonMFD_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkNonMFD.CheckedChanged
        If On_load = True Then Exit Sub
        Select Case Me.DtpMFD.Enabled
            Case True
                Me.DtpMFD.Enabled = False
            Case False
                Me.DtpMFD.Enabled = True

        End Select
    End Sub

    Private Sub TxtWarantry_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtWarantry.EditValueChanged

    End Sub

    Private Sub TxtVat_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtVat.EditValueChanged
        Update_Price()
    End Sub

    Private Sub CboItemType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboItemType.SelectedIndexChanged
        Try
            If On_load = True Then Exit Sub
            Dim DT_Item As New DataTable
            Strsql = "Select standdardprice,itemimage from itemtype where itemnumber = " & CboItemType.SelectedValue

            DT_Item = _mysql.GetMYSQLDataTable(Strsql, "itemtype")
            Dim MyImgFile As String = DT_Item.Rows(0).Item("itemimage").ToString
            If MyImgFile <> "" Then
                Me.TxtImagepart.Text = MyImgFile
                Dim bm As New Bitmap(Me.TxtImagepart.Text)
                PictureEdit1.Image = bm
            End If
            Me.NudStandardPrice.EditValue = DT_Item.Rows(0).Item("standdardprice")

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "CboItemType_SelectedIndexChanged"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub Cbosection_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cbosection.SelectedIndexChanged
        If On_load = True Then Exit Sub
        My.Settings.Current_Item_Section = Cbosection.SelectedValue
        Load_ProductType(Me.Cbosection.SelectedValue)
    End Sub

    Private Sub CboCategory_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboCategory.SelectedIndexChanged
        If On_load = True Then Exit Sub
        My.Settings.Current_Item_Category = CboCategory.SelectedValue
        Load_ItemType()
    End Sub
End Class